/*
Copyright 2016 Tomer Goldstein

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.tomergoldst.tooltipsdemo;

import com.ruffian.library.widget.component.RRadioButton;
import com.tomergoldst.tooltipshm.ToolTip;
import com.tomergoldst.tooltipshm.ToolTipsManager;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice implements
        Component.ClickedListener {
    private DependentLayout dlInitLayout;
    private TextField mEditText;
    private Text mTextView;
    private Text rightImageView;
    private Text leftImageView;
    private Text idAboveImageview;
    private Text idBelowimageView;
    private Text inputHint;
    private Button mAboveBtn;
    private Button mBelowBtn;
    private Button mLeftToBtn;
    private Button mRightToBtn;
    private RRadioButton checkboxLeft;
    private RRadioButton checkboxCenter;
    private RRadioButton checkboxRight;
    private int tag;
    private List<RRadioButton> checkboxes;
    private List<Button> buttons;
    private List<Text> texts;
    private List<DependentLayout> dependentLayouts;
    private List<Image> images;
    private ToolTipsManager mToolTipsManager;
    private ToolTip.Builder builder;
    private Image imaAboveCenter;
    private Image imaAboveLeft;
    private Image imaAboveRight;
    private Image imaRightCenter;
    private Image imaBelowLeft;
    private Image imaBelowCenter;
    private Image imaBelowRight;

    private DependentLayout dlAboveCenter;
    private DependentLayout dlAboveleft;
    private DependentLayout dlAboveRight;
    private DependentLayout leftDlView;
    private DependentLayout rightDlView;

    private DependentLayout dlBelowCenter;
    private DependentLayout dlBelowLeft;
    private DependentLayout dlBelowRight;

    private Text aboveViewleft;
    private Text aboveViewRight;
    private Image imaLeftCenter;

    private Text belowImageLeft;
    private Text belowImageRight;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_button_above:
                tag = 1;
                builder = new ToolTip.Builder(this, mTextView, imaAboveCenter);
                mToolTipsManager.show(tag, builder.build());
                break;
            case ResourceTable.Id_button_below:
                tag = 2;
                builder = new ToolTip.Builder(this, mTextView, imaAboveCenter);
                mToolTipsManager.show(tag, builder.build());
                break;
            case ResourceTable.Id_button_left_to:
                tag = 3;
                builder = new ToolTip.Builder(this, mTextView, imaAboveCenter);
                mToolTipsManager.show(tag, builder.build());
                break;
            case ResourceTable.Id_button_right_to:
                tag = 4;
                builder = new ToolTip.Builder(this, mTextView, imaAboveCenter);
                mToolTipsManager.show(tag, builder.build());
                break;
            case ResourceTable.Id_text_view:
                mToolTipsManager.dismissAll();
                break;
            case ResourceTable.Id_dl_init_layout:
                mToolTipsManager.dismissDialog();
                break;
            case ResourceTable.Id_above_image_view:
                break;
            default:

                break;
        }
    }

    // Control initialization
    private void initView() {
        dlAboveRight = (DependentLayout) findComponentById(ResourceTable.Id_dl_above_right);
        dlAboveleft = (DependentLayout) findComponentById(ResourceTable.Id_dl_above_left);
        dlAboveCenter = (DependentLayout) findComponentById(ResourceTable.Id_dl_above);
        leftDlView = (DependentLayout) findComponentById(ResourceTable.Id_left_dl_view);
        rightDlView = (DependentLayout) findComponentById(ResourceTable.Id_right_dl_view);
        dlBelowLeft = (DependentLayout) findComponentById(ResourceTable.Id_dl_below_left);
        dlBelowCenter = (DependentLayout) findComponentById(ResourceTable.Id_dl_below_center);
        dlBelowRight = (DependentLayout) findComponentById(ResourceTable.Id_dl_below_right);
        imaAboveCenter = (Image) findComponentById(ResourceTable.Id_ima_above_center);
        imaAboveLeft = (Image) findComponentById(ResourceTable.Id_ima_above_left);
        imaAboveRight = (Image) findComponentById(ResourceTable.Id_ima_above_right);
        imaLeftCenter = (Image) findComponentById(ResourceTable.Id_ima_left_center);
        imaRightCenter = (Image) findComponentById(ResourceTable.Id_ima_right_center);
        imaBelowLeft = (Image) findComponentById(ResourceTable.Id_ima_below_left);
        imaBelowCenter = (Image) findComponentById(ResourceTable.Id_ima_below_center);
        imaBelowRight = (Image) findComponentById(ResourceTable.Id_ima_below_right);
        aboveViewleft = (Text) findComponentById(ResourceTable.Id_above_view_left);
        aboveViewRight = (Text) findComponentById(ResourceTable.Id_above_view_right);
        belowImageLeft = (Text) findComponentById(ResourceTable.Id_below_image_left);
        belowImageRight = (Text) findComponentById(ResourceTable.Id_below_image_right);
        mToolTipsManager = new ToolTipsManager();
        mTextView = (Text) findComponentById(ResourceTable.Id_text_view);
        mAboveBtn = (Button) findComponentById(ResourceTable.Id_button_above);
        mBelowBtn = (Button) findComponentById(ResourceTable.Id_button_below);
        mLeftToBtn = (Button) findComponentById(ResourceTable.Id_button_left_to);
        mRightToBtn = (Button) findComponentById(ResourceTable.Id_button_right_to);
        dlInitLayout = (DependentLayout) findComponentById(ResourceTable.Id_dl_init_layout);
        inputHint = (Text) findComponentById(ResourceTable.Id_text_input_hint);
        checkboxLeft = (RRadioButton) findComponentById(ResourceTable.Id_check_box_left);
        checkboxCenter = (RRadioButton) findComponentById(ResourceTable.Id_check_box_center);
        checkboxRight = (RRadioButton) findComponentById(ResourceTable.Id_check_box_right);
        dlInitLayout.setClickedListener(this::onClick);
        mAboveBtn.setClickedListener(this);
        mBelowBtn.setClickedListener(this);
        mLeftToBtn.setClickedListener(this);
        mRightToBtn.setClickedListener(this);
        mTextView.setClickedListener(this);
        rightImageView = (Text) findComponentById(ResourceTable.Id_right_image_view);
        leftImageView = (Text) findComponentById(ResourceTable.Id_left_image_view);
        idAboveImageview = (Text) findComponentById(ResourceTable.Id_above_image_view);
        idBelowimageView = (Text) findComponentById(ResourceTable.Id_below_image_view);
        mEditText = (TextField) findComponentById(ResourceTable.Id_text_input_edit_text);
        rightImageView.setClickedListener(this);
        leftImageView.setClickedListener(this);
        idAboveImageview.setClickedListener(this);
        idBelowimageView.setClickedListener(this);
        checkboxCenter.setChecked(true);
        initList();
    }

    private void initList() {
        editFocusChange();
        buttonAbovePositionInit();
        checkboxes = new ArrayList<>();
        checkboxes.add(checkboxLeft);
        checkboxes.add(checkboxCenter);
        checkboxes.add(checkboxRight);
        buttons = new ArrayList<>();
        buttons.add(mAboveBtn);
        buttons.add(mBelowBtn);
        buttons.add(mLeftToBtn);
        buttons.add(mRightToBtn);

        texts = new ArrayList<>();
        texts.add(idAboveImageview);
        texts.add(idBelowimageView);
        texts.add(leftImageView);
        texts.add(rightImageView);
        texts.add(mTextView);
        texts.add(aboveViewleft);
        texts.add(aboveViewRight);
        texts.add(belowImageLeft);
        texts.add(belowImageRight);
        dependentLayouts = new ArrayList<>();
        dependentLayouts.add(dlAboveleft);
        dependentLayouts.add(dlAboveCenter);
        dependentLayouts.add(dlAboveRight);
        dependentLayouts.add(dlInitLayout);
        dependentLayouts.add(leftDlView);
        dependentLayouts.add(rightDlView);
        dependentLayouts.add(dlBelowLeft);
        dependentLayouts.add(dlBelowCenter);
        dependentLayouts.add(dlBelowRight);
        images = new ArrayList<>();
        images.add(imaAboveLeft);
        images.add(imaAboveCenter);
        images.add(imaAboveRight);
        images.add(imaLeftCenter);
        images.add(imaRightCenter);
        images.add(imaBelowLeft);
        images.add(imaBelowCenter);
        images.add(imaBelowRight);
        mToolTipsManager = new ToolTipsManager(dependentLayouts, images, mEditText, texts,
                buttons, checkboxes);
    }

    // editText focus change
    private void editFocusChange() {
        mEditText.setFocusChangedListener(new Component.FocusChangedListener() {
            @Override
            public void onFocusChange(Component component, boolean isFocused) {
                if (isFocused) {
                    ShapeElement shapeElement = new ShapeElement();
                    shapeElement.setRgbColor(new RgbColor(255, 64, 129));
                    setCursorColor(mEditText);
                    mEditText.setBasement(shapeElement);
                    mEditText.setHintColor(new Color(ResourceTable.Color_colorRed));
                    animateDismiss();

                    inputHint.setTextColor(new Color(Color.rgb(255, 64, 129)));
                    AnimatorProperty animatorProperty = inputHint.createAnimatorProperty();
                    animatorProperty.moveByY(-60).moveByX(-50).scaleY(0.65f).scaleX(0.65f).setDuration(1000).start();
                }
            }
        });
    }

    // set cursor color
    private void setCursorColor(TextField styleEditText) {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(ShapeElement.LINE);
        shapeElement.setRgbColor(new RgbColor(255, 64, 129));
        shapeElement.setGradientOrientation(ShapeElement.Orientation.TOP_TO_BOTTOM);
        shapeElement.setStroke(styleEditText.getTextSize(), RgbColor.fromArgbInt(Color.rgb(255, 0, 0)));
        styleEditText.setCursorElement(shapeElement);
    }

    // button above position
    private void buttonAbovePositionInit() {
        dlInitLayout.setVisibility(Component.VISIBLE);
        double leftAbovey = (double) mTextView.getContentPositionY() - (double)idAboveImageview.getEstimatedHeight();
        double leftAbovex =(double) mTextView.getContentPositionX() + (double)mTextView.getEstimatedWidth() / 2 - (double)idAboveImageview.getEstimatedWidth() / 2;
        idAboveImageview.setContentPosition((float) leftAbovex, (float)leftAbovey);
        AnimatorProperty abovePopup = new AnimatorProperty();
        abovePopup.alphaFrom(0).alpha(90).setCurveType(new Animator.CurveType().ANTICIPATE).setDuration(2000);
        abovePopup.setTarget(dlInitLayout);
        abovePopup.start();
    }

    // animates dismiss
    private void animateDismiss() {
        AnimatorProperty animatorValue = new AnimatorProperty();
        animatorValue.alphaFrom(0).alpha(0).setCurveType(Animator.CurveType.ANTICIPATE_OVERSHOOT).setDuration(500);
        dlInitLayout.setVisibility(Component.HIDE);
        dlAboveRight.setVisibility(Component.HIDE);
        dlAboveleft.setVisibility(Component.HIDE);
        dlAboveCenter.setVisibility(Component.HIDE);
        rightDlView.setVisibility(Component.HIDE);
        leftDlView.setVisibility(Component.HIDE);
        dlBelowLeft.setVisibility(Component.HIDE);
        dlBelowCenter.setVisibility(Component.HIDE);
        dlBelowLeft.setVisibility(Component.HIDE);
    }
}
